
elec <- read.csv("data/eggers_spirling/elections.csv")
cons <- read.csv("data/eggers_spirling/constituencies.csv")
rets <- read.csv("data/eggers_spirling/election_returns.csv")

all <- elec %>% 
    mutate(date2 = date(date)) %>%
    subset(year(date2) == 1910 & month(date2) == 12 ) %>%
    select(all_of(c("election_id", "constituency.id", "electors"))) %>%
    rename(constituency_id = constituency.id ) %>% 
    left_join(cons, by = "constituency_id") %>%
    subset(county_name %in% c("Yorkshire West Riding", "Norfolk", "Surrey", "Gloucestershire")) %>%
    left_join(rets, by = "election_id")

allsum <- all %>%
    subset(unopposed == 0) %>%
    group_by(county_name, party) %>%
    summarise(n = n(), tot_vote_party = sum(votes), .groups = "drop") %>%
    group_by(county_name) %>%
    mutate(shares = tot_vote_party/sum(tot_vote_party)) %>%
    mutate(sanity = sum(shares))

turnout <- all %>%
    subset(unopposed == 0) %>%
    group_by(county_name, constituency_id, electors) %>% 
    summarize_at(c("votes"), sum, na.rm = T) %>%
    mutate(turnout = votes/electors) %>% 
    ungroup() %>% group_by(county_name) %>% 
    summarise(turnout = mean(turnout), tot_elec = sum(electors), .groups = "drop") %>%
    subset(county_name %in% c("Yorkshire West Riding", "Norfolk", "Surrey", "Gloucestershire"))

allsum <- left_join(allsum, turnout, by = "county_name")

write.csv(allsum, "results/table/app_tab_b01_a.csv")
rm(elec, cons, all, allsum)

## demographics

### our counties
cen11  <- read.csv("data/1911_census_regdis/1911_rsd_data.csv")

censtat <- cen11 %>%
    subset(REGCNTY %in% c("YORKSHIRE WEST RIDING", "NORFOLK", "SURREY", "GLOUCESTERSHIRE")) %>%
      group_by(REGCNTY, TYPE) %>%
    summarise(n = n(), pop_cty_type = sum(POP), .groups = "drop") %>%
    group_by(REGCNTY ) %>% 
    mutate(pct_type = n/sum(n)) %>% #share of each reg district of this type
    mutate(pop_cty = sum(pop_cty_type)) %>% # total pop in county
    mutate(pct_pop_type = pop_cty_type / pop_cty) # share of pop in each type per county

write.csv(censtat, "results/table/app_tab_b01_b1.csv")

### english stats 

counties_in_wales <- c(
  "ANGLESEY",
  "BRECKNOCKSHIRE",
  "CAERNARFONSHIRE",
  "CARDIGANSHIRE",
  "CARMARTHENSHIRE",
  "DENBIGHSHIRE",
  "FLINTSHIRE",
  "GLAMORGANSHIRE",
  "MERIONETHSHIRE",
  "MONMOUTHSHIRE",
  "MONTGOMERYSHIRE",
  "PEMBROKESHIRE",
  "RADNORSHIRE"
)

censtat <- cen11 %>%
    subset(!(REGCNTY %in% counties_in_wales)) %>%
    group_by(TYPE) %>%
    summarise(n = n(), pop_ctry_type = sum(POP), .groups = "drop") %>%
    mutate(pct_type = n/sum(n)) %>% #share of each reg district of this type
    mutate(pop_ctry = sum(pop_ctry_type)) %>% # total pop in county
    mutate(pct_pop_type = pop_ctry_type / pop_ctry) # share of pop in each type per county
censtat$country <- "England"

write.csv(censtat, "results/table/app_tab_b01_b2.csv")

rm(cen11, censtat)